$(document).ready(function () {
    validateRule();
});

$.validator.setDefaults({
    submitHandler: function () {
        ajaxLogin();
    }
});

function ajaxLogin() {
    var index = layer.load(1);

    $.ajax({
        cache: true,
        type: "POST",
        url: '/ajaxLogin',
        data: $('#LoginForm').serialize(),
        async: false,
        error: function (request) {
            layer.close(index);
            layer.alert("Connection error" + request.message);
        },
        success: function (data) {
            setTimeout(200000);
            layer.close(index);
            parent.layer.msg(data.msg);
            if (data.code === 0) {
                window.location.href = "/index";
            }
        }
    });
    alert("sssssssssss")
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#LoginForm").validate({
        rules: {
            username: {
                required: true
            },
            password: {
                required: true
            },
            validateCode: {
                required: true,
                maxlength: 5

            }
        },
        messages: {
            username: {
                required: icon + "请输入您的用户名"
            },
            password: {
                required: icon + "请输入您的密码"
            },
            validateCode: {
                required: icon + "请输入验证码",
                maxlength: "验证码最多为五位"
            }
        }
    })
}

// 刷新图片
function changeImg() {
    var imgSrc = $("#randomCode");
    var src = imgSrc.attr("src");
    imgSrc.attr("src", changeUrl(src));
}

//为了使每次生成图片不一致，即不让浏览器读缓存，所以需要加上时间戳
function changeUrl(url) {
    var timestamp = (new Date()).valueOf();
    var index = url.indexOf("?", url);
    if (index > 0) {
        url = url.substring(0, url.indexOf("?", url));
    }
    if ((url.indexOf("&") >= 0)) {
        url = url + "×tamp=" + timestamp;
    } else {
        url = url + "?timestamp=" + timestamp;
    }
    return url;
}